import { createAction } from 'redux-actions';
import * as types from '../constants/ActionTypes';
import * as Api from '../utils/WebAPIUtils.js';
import Tool from '../utils/ToolUtils.js';

const saveMsg = createAction(types.SAVE_ERROR_MSG, data => data);
const fetchOneCategory = createAction(types.GET_ONE_CATEGORY, data => data);
const pushCategoryAction = {};
pushCategoryAction.getPushCategoryAction = createAction(types.FETCH_TEST_CATEGORY, data => data);
pushCategoryAction.storageChecked = createAction(types.STORAGE_CHECKED, data => data);
pushCategoryAction.fetchPushCategoryList = (data) => (dispath) =>
  Tool.post(Api.pushCategory.query, data, (res) => {
    dispath(pushCategoryAction.getPushCategoryAction(res));
  });

pushCategoryAction.restorePushCategory = createAction(types.RESTORE_PUSH_CATEGORY, data => data);

// 新增测试包信息
// ==================
pushCategoryAction.savePushCategory = (data, router) => (dispath) =>
  Tool.post(Api.pushCategory.save, data, (res) => {
    if (res.code === 200) {
      dispath(saveMsg(res));
      router.push({ pathname: '/testCategory/testResult' });
    } else if (res.code === 90001) {
      console.log('asdfasd');
    }
  }, (error) => { console.log('error'); });

pushCategoryAction.getOneCategory = (data, router) => (dispath) =>
  Tool.post(Api.testCategory.getOne, data, (res) => {
    if (res.code === 200) {
      dispath(fetchOneCategory(res.data));
    } else if (res.code === 90001) {
      console.log('asdfasd');
    }
  }, (error) => { console.log('error'); });

// 检测测试包名
// ==================
pushCategoryAction.checkPackageName = (data, router) => (dispath) =>
  Tool.post(Api.pushCategory.checkTypename, data, (res) => {
    if (res.code === 200) {
      dispath(saveMsg(res));
    }
  }, (error) => { console.log('error'); });

export default pushCategoryAction;



